<?php
namespace app\common\model;

class SystemSetting extends Common
{
    public $skeys = [
        'website_name' => [
            'name' => '网站名称',
            'value' => '一个新的站点'
        ],
        'website_address' => [
            'name' => '公司地址',
            'value' => '秦岭路荣成大厦908'
        ],
        'website_location' => [
            'name' => '公司坐标',
            'value' => ''
        ],
        'website_logo'=>[
            'name'=>'网站logo',
            'value'=>'',
        ],
        'website_favicon'=>[
            'name'=>'地址栏图标',
            'value'=>'',
        ],
        'website_host'=>[
            'name'=>'网站网址',
            'value'=>'http://www.jihainet.com/',
        ],
        'website_keyword'=>[
            'name'=>'网站关键词',
            'value'=>'商城系统|新零售系统|微信分销系统|B2B2C商城系统|多用户商城系统|小程序商城|二次发开|智慧餐饮',
        ],
        'website_description'=>[
            'name'=>'网站描述',
            'value'=>'河南吉海网络科技有限公司是专业的电商解决方案及技术提供商,Jshop系列电商系统支持多站点、分布式架构,为传统企业及电商企业提供完善的包括移动电商、微信分销、多商家入驻、小程序、智慧餐饮、官网资讯/品牌展示等解决方案。',
        ],
        'website_copyright'=>[
            'name'=>'版权信息',
            'value'=>'河南吉海网络科技有限公司 版权所有 2017-2018豫ICP备17046546号-1',
        ],
        'website_contact'=>[
            'name'=>'联系方式',
            'value'=>'0371-67584810',
        ],
        'website_mail' => [
            'name' => '公司邮箱',
            'value' => 'service@jihainet.com'
        ]
    ];


    /**
     * 设置参数
     * @param $skey
     * @param $value
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setValue($skey, $value)
    {
        $result = $this->check($skey, $value);
        if(!$result['status']){
            return $result;
        }

        $info = $this->where(array('skey'=>$skey))->find();
        if($info){
            $info->value = $value;
            $info->save();
        }else{
            $model = new $this;
            $model->save([
                'skey' => $skey,
                'value' => $value
            ]);
        }
        $result['status'] = true;
        return $result;
    }

    /**
     * 取得参数
     * @param $skey
     * @return mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getValue($skey)
    {
        $info = $this->where(array( 'skey' => $skey))->find();
        if($info){
            return $info['value'];
        }else{
            if(isset($this->skeys[$skey]['value'])){
                return $this->skeys[$skey]['value'];
            }else{
                return "";
            }
        }
    }

    /**
     * 参数校验
     * @param $skey
     * @param $value
     * @return array
     */
    public function check($skey, $value)
    {
        $result = array(
            'status' => false,
            'data' => '',
            'msg' => ''
        );
        if (!isset($this->skeys[$skey])) {
            return error_code(10008);
        }

        if($skey == 'shop_name'){
            if($value == ''){
                $result['msg'] = "店铺名称不能为空";
                return $result;
            }
        }

        if($skey == 'shop_mobile'){
            $isMob="/^1[34578]{1}\d{9}$/";

            $isTel="/^([0-9]{3,4}-)?[0-9]{7,8}$/";

            if(!preg_match($isMob,$value) && !preg_match($isTel,$value))
            {
                $result['msg'] = '联系方式号码格式错误';
                return $result;
            }
        }

        $result['status'] = true;
        return $result;
    }

    /**
     * 取得全部参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAll()
    {
        $list = $this->where([])->select();
        foreach($this->skeys as $k => $v){
            foreach($list as $info){
                if($info['skey'] == $k){
                    $this->skeys[$k]['value'] = $info['value'];
                    break;
                }
            }
        }
        return $this->skeys;
    }

    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }
        $tableWhere = $this->tableWhere($post);
        $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;

        return $re;
    }

    /**
     * 根据输入的查询条件，返回所需要的where
     * @param $post
     * @return mixed
     */
    protected function tableWhere($post)
    {
        $where = [];

        if(isset($post['skey']) && $post['skey'] != ""){
            $where[] = ['skey', 'eq', $post['skey']];
        }
        if(isset($post['value']) && $post['value'] != ""){
            $where[] = ['value', 'like', '%'.$post['value'].'%'];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = [];
        return $result;
    }

    /**
     * 根据查询结果，格式化数据
     * @param $list
     * @return mixed
     */
    protected function tableFormat($list)
    {
        foreach($list as $k => $v){
            if(isset($this->skeys[$v['skey']])){
                $list[$k]['key_name'] = $this->skeys[$v['skey']]['name'];
            }else{
                $list[$k]['key_name'] = "";
            }
        }
        return $list;
    }
}